package ophiux.wechat.template.util;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 
 * 
 * 文件名称: DateUtil
 * 内容摘要: 时间工具类
 * 创 建 人: sunrise
 * 创建日期:2015-4-14 上午10:46:15
 * 修改记录1: // 修改历史记录，包括修改日期、修改者及修改内容
 *   修改日期：
 *   版 本 号：
 *   修 改 人：
 *   修改内容：
 * 修改记录2：…
 *
 */
public class DateUtil {
	
	
	  private static String el ="^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
	
	   public  enum DateFormat{
		   /**
		    * yyyy-MM-DD HH:mm:ss
		    */
		   YYYY_MM_DD_HH_mm_ss,
		   /**
		    * yyyyMMDDHHmmss
		    */
		   YYYYMMDDHHmmss,
		   /**
		    *HH:mm:ss
		    */
		   HH_mm_ss,
		   /**
		    * YYYY-MM-DD
		    */
		   YYYY_MM_DD, 
		   /**
		    * HH:MM
		    */
		   HHmm,
		   /**
		    * YYYYMMDDHHmmssSSS
		    */
		   YYYYMMDDHHmmssSSS;
		   
	   }
	
	//yyyyMMddHHmmssSSSS  
	   
	private final static SimpleDateFormat df_yyyy_MM_dd=new SimpleDateFormat("yyyy-MM-dd");
	
	private final static SimpleDateFormat df_HH_mm_ss=new SimpleDateFormat("HH_mm_ss");
	private final static SimpleDateFormat YYYY_MM_DD_HH_mm_ss=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private final static SimpleDateFormat YYYYMMDDHHmmss=new SimpleDateFormat("yyyyMMddHHmmss");
	private final static SimpleDateFormat YYYYMMDDHHmmssSSS=new SimpleDateFormat("yyyyMMddHHmmssSSS");
	private final static SimpleDateFormat HHMM=new SimpleDateFormat("HH:mm");
	@SuppressWarnings("unused")
	private final static SimpleDateFormat df_yyyy_MM_dd_HMS_S=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss:SSSS");

	/**
	 * 
	 * Description: 格式化字符串时间
	 * @param date
	 * @param dateFormat
	 * @return
	 */
	public static String getStrDate(Date date,DateFormat dateFormat){
		
		if(date == null ){
			date = new Date();
		}
		switch (dateFormat) {
		  case YYYY_MM_DD_HH_mm_ss:
			 return YYYY_MM_DD_HH_mm_ss.format(date);
			
          case YYYY_MM_DD:
			return df_yyyy_MM_dd.format(date);
			
          case HH_mm_ss:
  			return df_HH_mm_ss.format(date);
          case YYYYMMDDHHmmss:
        	  return YYYYMMDDHHmmss.format(date);
          case YYYYMMDDHHmmssSSS:
        	  return YYYYMMDDHHmmssSSS.format(date);
          case HHmm:
  			 return HHMM.format(date);
		default:
			break;
		}
		return "";
	}	
	/**
	 * 
	 * Description: 格式化字符串时间
	 * @param date
	 * @param dateFormat
	 * @return
	 * @throws ParseException 
	 */
	public static Date getDate(String dateStr,DateFormat dateFormat) throws ParseException{
		
		
		switch (dateFormat) {
		  case YYYY_MM_DD_HH_mm_ss:
			 return YYYY_MM_DD_HH_mm_ss.parse(dateStr);
			
          case YYYY_MM_DD:
			return df_yyyy_MM_dd.parse(dateStr); 
          case HHmm:
  			return HHMM.parse(dateStr);
          case YYYYMMDDHHmmss:
    			return YYYYMMDDHHmmss.parse(dateStr);

		default:
			return df_yyyy_MM_dd.parse(dateStr);
			
		}
	
	}	
		
		
	  private DateUtil() {
	  }

	   /**
	    * java 偏移时间
	    * @param starDate
	    * @param leadValue
	    * @return
	    */
	  public static Date getLeadDay(Date starDate,int leadValue){
			GregorianCalendar gc=new GregorianCalendar(); 
			if(starDate != null){
				gc.setTime(starDate);
			}
			gc.add(Calendar.DATE, leadValue);
			return gc.getTime();
			
		}   
	  
	  /**
	   * 返回时间间隔
	   * @param date1
	   * @param date2
	   * @return
	   */
	  public static int getDateLead(String date1,String date2){
			String s1 = date1;
			String s2 = date2;
			SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd");
			ParsePosition pos = new ParsePosition(0);
			ParsePosition pos1 = new ParsePosition(0);
			Date dt1=formatter.parse(s1,pos);
			Date dt2=formatter.parse(s2,pos1);
			long l = (dt1.getTime() - dt2.getTime())/(3600*24*1000);
			return (int) ((dt1.getTime() - dt2.getTime())/86400000);
	  }
	  /**
	   * 获取指定日期是星期几
	   * 参数为null时表示获取当前日期是星期几
	   * @param date
	   * @return
	   */
	 public static String getWeekOfDate(Date date) {      
	     String[] weekOfDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};        
	     Calendar calendar = Calendar.getInstance();      
	     if(date != null){        
	          calendar.setTime(date);      
	     }        
	     int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;      
	     if (w < 0){        
	         w = 0;      
	     }      
	     return weekOfDays[w];    
	 }
	 
	 /**
	   * 获取指定日期是星期几
	   * 参数为null时表示获取当前日期是星期几
	   * @param date
	   * @return
	 * @throws ParseException 
	   */
	 public static String getWeekOfDate(String dateStr) throws ParseException {      
	     String[] weekOfDays = {"周末", "周一", "周二", "周三", "周四", "周五", "周六"};        
	     Calendar calendar = Calendar.getInstance();    
	     Date  date= df_yyyy_MM_dd.parse(dateStr);
	     if(date != null){        
	          calendar.setTime(date);      
	     }        
	     int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
//	     System.out.println(w);
	     if (w < 0){        
	         w = 0;      
	     }  
	     
	     return weekOfDays[w];    
	 }
	 
	 public static boolean checkValues(String datetime){
		 Pattern p = Pattern.compile(el);         
         Matcher m = p.matcher(datetime);         
         boolean b = m.matches();   
         return b;
	 }
	 
	 
		/**
		 * 获取上午下午标示
		 * @param fore
		 * @param after
		 * @return 1 上午 2 下午 3 晚上
		 */
	 public static int getNoonFlag(int fore,int after) {
			int flag;
			
			//当前时间
			Date now = new Date();
			System.out.println("当前:"+DateUtil.getStrDate(now, DateFormat.YYYY_MM_DD_HH_mm_ss));
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			//上午时间
			calendar.set(Calendar.HOUR_OF_DAY, fore);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			Date foreDate = calendar.getTime();
			System.out.println("上午:"+DateUtil.getStrDate(foreDate, DateFormat.YYYY_MM_DD_HH_mm_ss));
			
			//下午时间
			calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.set(Calendar.HOUR_OF_DAY, after);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			Date afterDate = calendar.getTime();
			System.out.println("下午:"+DateUtil.getStrDate(afterDate, DateFormat.YYYY_MM_DD_HH_mm_ss));
			//比较时间
			if(now.compareTo(foreDate) <= 0) {
				//上午 上午下午都可以挂号
				flag = 1;
			}else if(now.compareTo(foreDate) > 0 && now.compareTo(afterDate) <= 0) {
				//下午阶段 只能下午的号
				flag = 2;
			}else  {
				//3已过下午 不能挂当日号
				flag = 3;
			}
			
			System.out.println("flag:"+flag);
			return flag;
		}
	 
	 /**
		 * 获取上午下午标示
		 * @param fore
		 * @param after
		 * @return 1 上午 2 下午 3 晚上
		 */
	 public static int getNoonFlag(String fore,String after) {
			int flag;
			
			//当前时间
			Date now = new Date();
			System.out.println("当前:"+DateUtil.getStrDate(now, DateFormat.YYYY_MM_DD_HH_mm_ss));
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			
			int forehour = Integer.parseInt(fore.substring(0,2));
			int foreminute = 0;
			if(fore.trim().length()>2){//存在分钟
				foreminute = Integer.parseInt(fore.substring(2,fore.length()-1));
			}
			//上午时间
			calendar.set(Calendar.HOUR_OF_DAY, forehour);
			calendar.set(Calendar.MINUTE, foreminute);
			calendar.set(Calendar.SECOND, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			Date foreDate = calendar.getTime();
			System.out.println("上午:"+DateUtil.getStrDate(foreDate, DateFormat.YYYY_MM_DD_HH_mm_ss));
			
			//下午时间
			calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			int afterhour = Integer.parseInt(after.substring(0,2));
			int afterminute = 0;
			if(after.trim().length()>2){//存在分钟
				afterminute = Integer.parseInt(after.substring(3,after.length()));
			}
			calendar.set(Calendar.HOUR_OF_DAY, afterhour);
			calendar.set(Calendar.MINUTE, afterminute);
			calendar.set(Calendar.SECOND, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			Date afterDate = calendar.getTime();
			System.out.println("下午:"+DateUtil.getStrDate(afterDate, DateFormat.YYYY_MM_DD_HH_mm_ss));
			//比较时间
			if(now.compareTo(foreDate) <= 0) {
				//上午 上午下午都可以挂号
				flag = 1;
			}else if(now.compareTo(foreDate) > 0 && now.compareTo(afterDate) <= 0) {
				//下午阶段 只能下午的号
				flag = 2;
			}else  {
				//3已过下午 不能挂当日号
				flag = 3;
			}
			
			System.out.println("flag:"+flag);
			return flag;
		}
	 public static void main(String[] args) {
//		 getNoonFlag("12", "17:30");
		try {
//			String week =  getWeekOfDate("2017-05-20");
			System.out.println(DateUtil.getStrDate(new Date(),DateUtil.DateFormat.YYYY_MM_DD_HH_mm_ss));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
